package teamproject.visualization.io;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.media.j3d.ImageComponent2D;
import javax.media.j3d.Texture;
import javax.media.j3d.Texture2D;

import com.sun.j3d.utils.image.TextureLoader;

public class ImageProvider {

	private static Map<String, Texture2D> images = new HashMap<String, Texture2D>();

	public static Texture2D getImage(String filename) {
		if (!images.containsKey(filename)) {
			BufferedImage bufferedImage = null;
			try {
				bufferedImage = ImageIO.read(ImageProvider.class
						.getResource(filename));
			} catch (IOException e) {
				e.printStackTrace();
			}

			TextureLoader loader = new TextureLoader(bufferedImage);
			ImageComponent2D image = loader.getImage();
			Texture2D texture = new Texture2D(Texture.BASE_LEVEL, Texture.RGBA,
					image.getWidth(), image.getHeight());
			texture.setImage(0, image);

			images.put(filename, texture);
		}

		return images.get(filename);
	}
}
